const {getHttpCart} = require("../../models/shop");

Page({
  data: {
    isChecked:true,
    carts:[],
    sum:""
  },

  onLoad(){
    getHttpCart({
      success:res =>{
        this.setData({
          carts:res.data
        })
      }
    })
  },

  onCount(event){
    var carts = this.data.carts;
    var count = event.detail;
    console.log(count);
    var id = event.currentTarget.dataset.id;
    var item = carts.find(item => item.id ==id);
    item.productCount = count;
    this.setData({
      carts
    })
  },


  onItemChange(event){
    this.handleItem("isSelected",event)
  },

  handleItem(attr,event){
    var carts = this.data.carts;
    var value = event.detail;
    console.log(value);
    var id = event.currentTarget.dataset.id;
    var item = carts.find(item=>item.id ==id);
    item[attr] = value;
    this.setData({
      carts
    })
  },
  

  onSum(event){
    var carts = this.data.carts;
    var value = event.detail;
    console.log(value);
    carts.forEach(item=>{
      item.isSelected = value;
    })
    this.setData({
      carts
    })
  },

/*   onChange(event){
    this.setData({
      isChecked:event.detail
    })
    console.log(event.detail);
  }, */

 /*  handleChange(event){
    this.setData({
      isChecked:!this.data.isChecked
    })
  }, */

 
  
})
